package com.web.webstart.base.controller;

import com.alibaba.fastjson.JSON;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.entity.XaCmsResource;
import com.web.webstart.base.security.XaSecurityMetadataSourceService;
import com.web.webstart.base.service.XaCmsResourceService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.result.CmsResult;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.vo.SelectOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: XaCmsResourceController.java
 * @Package com.web.shengmilu.business.controller
 * @Description: 资源控制器
 * @date 2014年8月2日 上午11:42:16
 */
@Controller
@RequestMapping("xaCmsResource")
public class XaCmsResourceController {

    @Autowired
    private XaCmsResourceService xaCmsResourceService;

    /**
     * getAllResource
     * 列表查询所有资源
     * @param nextPage 下一次，页数从0开始
     * @param pageSize
     * @param showType
     * @param resourceName
     * @author duandazhi 修改
     * @date 2017/1/3 上午11:25
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "showResource")
    public XaResult<Page<XaCmsResource>> getAllResource(
            @RequestParam Integer nextPage,
            @RequestParam Integer pageSize,
            @RequestParam String resourceName,
            @RequestParam(required = false) Integer showType) {
        if (XaUtil.isEmpty(resourceName)) {
            resourceName = "";
        }
        XaResult<Page<XaCmsResource>> data = xaCmsResourceService.findXaCmsResourceByConditon(resourceName, showType, nextPage, pageSize);
        return data;
    }

    /**
     * saveResource
     * 保存资源
     * @param xaCmsResource
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public CmsResult saveResource(@RequestBody XaCmsResource xaCmsResource) {
        xaCmsResource.setOrderNum(100);            //设置默认排序
        xaCmsResource.setStatus(XaConstant.ResourcesStatus.status_normal);
        xaCmsResource = xaCmsResourceService.saveXaCmsResource(xaCmsResource);
        XaSecurityMetadataSourceService.reset();    //刷新security中的资源列表
        return new CmsResult("新建资源成功");
    }

    /**
     * @return
     * @Title: getParentResource
     * @Description: 返回一级资源
     */
    @ResponseBody
    @RequestMapping(value = "showParent/{rid}", method = RequestMethod.GET)
    public SelectOptionVo getParentResource(@PathVariable Long rid) {
        List<XaCmsResource> result = xaCmsResourceService.getMenuLevelResource();
        SelectOptionVo sov = new SelectOptionVo();
        sov.setOptionItem(result);
        if (rid != null) {
            XaCmsResource resource = xaCmsResourceService.findXaCmsResourceById(rid);
            sov.setSelectedId(resource);
        } else {
            sov.setSelectedId("");
        }
        return sov;
    }

    /**
     * @param ids
     * @param request
     * @param response
     * @return
     * @Title: batchDelResource
     * @Description: 批量删除资源信息，假删资源。注意，删除资源时，需要将角色和资源之间的关系同时删掉
     */
    @ResponseBody
    @RequestMapping(value = "resource/{ids}", method = RequestMethod.DELETE)
    public CmsResult batchDelResource(@PathVariable String ids, HttpServletRequest request, HttpServletResponse response) {
        @SuppressWarnings("unchecked")
        Map<String, String> map = JSON.parseObject(ids, HashMap.class);
        String ids1 = map.get("ids");
        int code = xaCmsResourceService.delXaCmsResource(ids1);
        return new CmsResult(code,"删除资源成功");
    }


    /**updateResource
     * 更新一条资源记录
     * @param xaCmsResource
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public CmsResult updateResource(@RequestBody XaCmsResource xaCmsResource, HttpServletRequest request, HttpServletResponse response) {
        XaCmsResource oldResource = xaCmsResourceService.findXaCmsResourceById(xaCmsResource.getResourceId());
        CmsResult result;
        if (oldResource != null) {
            xaCmsResourceService.updateXaCmsResource(xaCmsResource);
            XaSecurityMetadataSourceService.reset();    //刷新security中的资源列表
            return new CmsResult("修改资源成功");
        } else {
            return CmsResult.error("修改资源不成功，" + xaCmsResource.getResourceId() + "对应的资源不存在");
        }
    }
}

